package com.tiancheng.trade.merchant.controller.open;

import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.Asserts;
import com.tiancheng.trade.commom.web.model.request.*;
import com.tiancheng.trade.commom.web.model.request.merchant.*;
import com.tiancheng.trade.commom.web.model.response.*;
import com.tiancheng.trade.merchant.channel.IdPhotoEnums;
import com.tiancheng.trade.merchant.enums.MerchantTypeEnums;
import com.tiancheng.trade.merchant.service.IAreaService;
import com.tiancheng.trade.merchant.service.IMerchantPayRegService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @Author: kellen
 * @Description: 商户进件 open
 * @Date: create in 2024/11/19 15:17
 */
@RestController
@RequestMapping("/open/merchantPayReg")
public class MerchantPayRegOpenController {

    @Resource
    private IMerchantPayRegService merchantPayRegService;
    @Resource
    private IAreaService areaService;

    /**
     * 文件上传
     *
     * @param file         附件
     * @param documentType 附件类型
     * @param regMode      进件模式：0-人工进件，1-自助进件
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    public Result<MerchantPayRegFileResVO> upload(@RequestPart("file") MultipartFile file,
                                                  @RequestParam(name = "termNo", required = false) String termNo,
                                                  @RequestParam(name = "documentType", required = false) String documentType,
                                                  @RequestParam(name = "regMode", required = false, defaultValue = "1") Integer regMode) {
        //把文件的内容存储到本地磁盘上
        if (StringUtils.isNotEmpty(documentType)) {
            final MerchantPayRegFileResVO payRegFileResVO = merchantPayRegService.upload(file, documentType, regMode, termNo);
            return Result.success(payRegFileResVO);
        }
        final MerchantPayRegFileResVO payRegFileResVO = merchantPayRegService.upload(file, regMode, termNo);
        return Result.success(payRegFileResVO);
    }

    /**
     * 文件删除
     *
     * @param merchantRegDeleteFileReqVO 删除文件
     * @return
     */
    @PostMapping("/deleteFile")
    public Result deleteFile(@Validated @RequestBody MerchantRegDeleteFileReqVO merchantRegDeleteFileReqVO) {
        //把文件的内容存储到本地磁盘上
        merchantPayRegService.deleteFile(merchantRegDeleteFileReqVO.getFileId());
        return Result.success();
    }

    /**
     * 查询上传的文件列表
     *
     * @param
     * @return
     * @throws IOException
     */
    @PostMapping("/queryFileList")
    @ResponseBody
    public Result<List<MerchantPayRegFileResVO>> queryFileList(@Validated @RequestBody MerchantReqQueryFileListVO params) throws IOException {
        //把文件的内容存储到本地磁盘上
        final List<MerchantPayRegFileResVO> files = merchantPayRegService.queryFileList(params.getFileIds());
        return Result.success(files);
    }

    /**
     * 商户注册-最新版
     *
     * @return
     */
    @PostMapping("/registerMerchant")
    public Result<MerchantRegAllInfoNewRes> registerMerchantNew(@Validated @RequestBody RegisterMerchantReqNewVO registerMerchantReqVO) {
        MerchantRegAllInfoNewRes merchantRegAllInfoNewRes = merchantPayRegService.registerMerchantNew(registerMerchantReqVO);
        return Result.success(merchantRegAllInfoNewRes);
    }

    /**
     * 商户注册
     *
     * @return
     * @throws IOException
     */
    @GetMapping("/queryByShopLic")
    public Result<MerchantRegAllInfoRes> queryRegisterMerchant(@RequestParam("shopLic") String shopLic, @RequestParam("orgPaymentChannelId") Long orgPaymentChannelId) throws IOException {
        //把文件的内容存储到本地磁盘上
        final MerchantRegAllInfoRes merchantPayRegResVOs = merchantPayRegService.queryRegisterMerchant(shopLic, orgPaymentChannelId);
        return Result.success(merchantPayRegResVOs);
    }


    /**
     * 商户进件状态查询
     *
     * @return
     * @throws IOException
     */
    @PostMapping("/queryMerchantRegStatus")
    public Result<MerchantRegStatusQueryRes> queryMerchantRegStatus(@Validated @RequestBody MerchantRegStatusQueryReqVO params) {
        MerchantRegStatusQueryRes merchantRegStatusQueryRes = merchantPayRegService.queryMerchantRegStatus(params);
        return Result.success(merchantRegStatusQueryRes);
    }

    /**
     * 所属支行查询接口
     *
     * @return
     */
    @PostMapping("/branchBankList")
    public Result<List<MerchantBranchBankListResVO>> branchBankList(@RequestBody @Validated MerchantBranchBankListReqVO merchantBranchBankListReqVO) {
        //把文件的内容存储到本地磁盘上
        final List<MerchantBranchBankListResVO> merchantPayRegResVOs = merchantPayRegService.branchBankList(merchantBranchBankListReqVO);
        return Result.success(merchantPayRegResVOs);
    }

    /**
     * 根据分级编码查询地区列表
     *
     * @param params 查询参数
     * @return
     */
    @PostMapping("/getRegion")
    public Result<List<QueryAreaRes>> queryByParentAreaCode(@RequestBody MerchantRegQueryRegionReqVO params) {
        if (StringUtils.isEmpty(params.getParentAreaCode())) {
            params.setParentAreaCode("0");
        }
        return Result.success(areaService.queryByParentAreaCode(params.getParentAreaCode()));
    }

    /**
     * 根据分级编码查询地区列表
     *
     * @param params 地区编码
     * @return
     */
    @PostMapping("/queryRegionByCodes")
    public Result<List<QueryAreaRes>> queryRegionByCodes(@RequestBody MerchantRegQueryRegionReqVO params) {
        Asserts.isNotEmpty(params.getAreaCodes(), "请传入地区编码");
        return Result.success(areaService.queryByAreaCode(params.getAreaCodes()));
    }

    /**
     * 行业信息列表
     *
     * @param params
     * @return
     */
    @PostMapping("/mccList")
    public Result<List<QueryMccRes>> mccList(@Validated @RequestBody MerchantRegQueryMccReqVO params) {
        return Result.success(merchantPayRegService.queryMcc(params));
    }

    /**
     * 查询行业信息-根据编码
     *
     * @param params 行业编码
     * @return
     */
    @PostMapping("/queryMccByCode")
    public Result<List<QueryMccRes>> queryMccByCode(@Validated @RequestBody MerchantRegQueryMccReqVO params) {
        Asserts.isNotEmpty(params.getMccCode(), "请传入行业编码");
        return Result.success(merchantPayRegService.queryMccByCode(params));
    }

    /**
     * 变更商户清分规则
     *
     * @param ruleReqVO
     * @return
     * @throws IOException
     */
    @PostMapping("/setSettleRule")
    public Result setSettleRule(@RequestBody @Validated MerchantPayRegSettleRuleReqVO ruleReqVO) throws IOException {
        //变更-商户证件变更提交
        merchantPayRegService.setSettleRule(ruleReqVO);
        return Result.success("设置商户清算规则成功");
    }

    //-------------------------------------------------------------------------------------------------------------------验证相关
    /**
     * 发起对公账户验证交易
     *
     * @param param
     * @return
     */
    @PostMapping("/requestAccountVerify")
    public Result<RequestAccountVerifyResVO> requestAccountVerify(@RequestBody @Validated RequestAccountVerifyRequest param) {
        RequestAccountVerifyResVO resVO = merchantPayRegService.requestAccountVerify(param);
        return Result.success(resVO);
    }

    /**
     * 发起对公账户验证交易
     *
     * @param param
     * @return
     */
    @PostMapping("/companyAccountVerify")
    public Result<CompanyAccountVerifyResVO> companyAccountVerify(@RequestBody @Validated RequestCompanyAccountVerifyRequest param) {
        CompanyAccountVerifyResVO res = merchantPayRegService.companyAccountVerify(param);
        return Result.success(res);
    }

    /**
     * 发起个体商户短信验证
     *
     * @param param
     * @return
     */
    @PostMapping("/smsSend")
    public Result<String> smsSend(@RequestBody @Validated MerchantRegSendSmsReqVO param) {
        merchantPayRegService.smsSend(param);
        return Result.success("短信发送成功！");
    }

    /**
     * 发起个体商户短信验证
     *
     * @param param
     * @return
     */
    @PostMapping("/smsVerify")
    public Result<String> smsVerify(@RequestBody @Validated MerchantRegSmsVerifyReqVO param) {
        merchantPayRegService.smsVerify(param);
        return Result.success("短信发送成功！");
    }

    /**
     * 前台签约接口
     *
     * @param param
     * @return
     */
    @PostMapping("/agreementSign")
    public Result<MerchantAgreementSignResVO> agreementSign(@RequestBody @Validated MerchantPayRegAgreementSignRequest param) {
        MerchantAgreementSignResVO res = merchantPayRegService.agreementSign(param);
        return Result.success(res);
    }

}
